package cn.kai.ums.controller;

import cn.kai.ums.mapper.UmsPublicCourseMapper;
import cn.kai.ums.model.UmsGrade;
import cn.kai.ums.model.UmsPublicCourse;
import cn.kai.ums.model.UmsTeacherCourse;
import cn.kai.ums.service.UmsGradeService;
import cn.kai.ums.service.UmsPublicCourseService;
import cn.kai.ums.service.UmsTeacherCourseService;
import cn.kai.ums.tools.JsonData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ums/v1/publiccourse")
@Api(tags = "选修课控制类")
public class PublicCourseController {

    @Autowired
    private UmsPublicCourseService umsPublicCourseService;

    @Autowired
    private UmsPublicCourseMapper umsPublicCourseMapper;

    @Autowired
    private UmsGradeService umsGradeService;

    @ApiOperation("学生选课方法")
    @PostMapping("/selectPublic")
    public JsonData selectPublic(@RequestParam String stuid, @RequestParam String couid){
        UmsGrade umsGrade = umsGradeService.getOnePub(stuid,couid);
        if(umsGrade==null){
            return JsonData.buildError(-1,"选课失败");
        }else {
            return JsonData.buildSuccess(umsGradeService.save(umsGrade));
        }
    }

    @ApiOperation("根据课程编号和课程名称进行模糊查询")
    @GetMapping
    public JsonData getPubList(@RequestParam(defaultValue = "1",required = false) Integer pageNum
            ,@RequestParam(defaultValue = "5",required = false) Integer pageSize
            ,@RequestParam(defaultValue = "",value = "likepub",required = false) String likepub){

        Page<UmsPublicCourse> umsPublicCoursePage = umsPublicCourseMapper.selectPage(new Page<>(pageNum,pageSize)
                , Wrappers.<UmsPublicCourse>lambdaQuery()
                        .like(UmsPublicCourse::getPublicCourseNo,likepub)
                        .or()
                        .like(UmsPublicCourse::getPublicCourseName,likepub));
        return JsonData.buildSuccess(umsPublicCoursePage);
    }


    @PostMapping
    public JsonData savePublic(@RequestBody UmsPublicCourse umsPublicCourse ){
        return JsonData.buildSuccess(umsPublicCourseService.save(umsPublicCourse));
    }

    @PutMapping
    public JsonData updatePublic(@RequestBody UmsPublicCourse umsPublicCourse){
        return JsonData.buildSuccess(umsPublicCourseService.updateById(umsPublicCourse));
    }

    @DeleteMapping
    public JsonData delPublic(@RequestParam Integer pubid){
        return JsonData.buildSuccess(umsPublicCourseService.removeById(pubid));
    }

    @PostMapping("/delBatchPublic")
    public JsonData delBatchPublic(@RequestBody List<Integer> pubids){

        return JsonData.buildSuccess(umsPublicCourseService.removeByIds(pubids));
    }

    @ApiOperation("学生个人的选修课信息")
    @GetMapping("/studenPub")
    public JsonData studenPub(@RequestParam String stuid){
        List<UmsPublicCourse> umsPublicCourses = umsPublicCourseService.studenPub(stuid);
        return JsonData.buildSuccess(umsPublicCourses);
    }

    @ApiOperation("教师个人的选修课信息")
    @GetMapping("/teacherPub")
    public JsonData teacherPub(@RequestParam String teaid){
        List<UmsPublicCourse> umsPublicCourses = umsPublicCourseService.teacherPub(teaid);
        return JsonData.buildSuccess(umsPublicCourses);

    }

}
